#!/bin/bash
if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash build.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

create_dir()
{
    local path=$1
    [[ "$path" =~ .*/$ ]] && path=${path%/*}
    if [[ ! -d "$path" && ! -f "$path" ]]; then mkdir -p $path; else path=$path`date "+%y%m%d%H%M%S"` && mkdir -p $path; fi
    echo $path
}

buildpath=$(create_dir $1)
installpath=$(create_dir $2)

yum install -y m4 environment-modules systemd-devel
source /etc/profile
module purge


#begin
#basic dependency software
yum install -y gcc gcc-c++ make automake
yum install -y git

#for zip compressed input: 
yum install -y boost-iostreams.aarch64
yum install -y boost-serialization.aarch64
yum install -y zlib zlib-devel bzip2 bzip2-devel xz-devel


set -e
dep_dir="dependency"
bash $dep_dir/bisheng-compiler-2.1.0.sh $buildpath $installpath
module use $installpath/AUGUSTUS/bisheng-compiler-2.1.0
module load $installpath/AUGUSTUS/bisheng-compiler-2.1.0/bisheng_modulefiles

#for compiling utilities cmake(version>3.6)
yum install -y cmake


#for compiling utilities htslib
#htslib的依赖软件
yum install -y libcurl libcurl-devel openssl openssl-devel boost-devel
#安装htslib
yum install -y htslib
yum install -y htslib-devel

#for compiling utilities samtools
#samtools的依赖软件
yum install -y ncurses ncurses-base ncurses-libs ncurses-devel
#安装samtools
yum install -y samtools 

#for compiling utilities bcftools 
bash $dep_dir/bcftools-1.9.sh $buildpath $installpath
module use $installpath/AUGUSTUS/bcftools-1.9
module load $installpath/AUGUSTUS/bcftools-1.9/bcftools_modulefiles

#for compiling utilities bamtools
bash $dep_dir/bamtools-2.5.2.sh $buildpath $installpath
module use $installpath/AUGUSTUS/bamtools-2.5.2
module load $installpath/AUGUSTUS/bamtools-2.5.2/bamtools_modulefiles

#for compiling utilities lp_solve
bash $dep_dir/lp_solve_5.5.sh $buildpath $installpath
module use $installpath/AUGUSTUS/lp_solve_5.5
module load $installpath/AUGUSTUS/lp_solve_5.5/lp_solve_modulefiles


#for comparative AUGUSTUS (multi-species, CGP): 
yum install -y gsl-devel.aarch64
yum install -y sqlite-devel.aarch64
yum install -y suitesparse-devel
bash $dep_dir/mysql1-3.3.0.sh $buildpath $installpath
module use $installpath/AUGUSTUS/mysql++-3.3.0
module load $installpath/AUGUSTUS/mysql++-3.3.0/mysql1_modulefiles


bash Augustus-3.4.0.sh $buildpath $installpath

mkdir -p pkg/Augustus-3.4.0-hpc && cp -r $installpath/AUGUSTUS/* pkg/Augustus-3.4.0-hpc/


cat>"pkg/Augustus-3.4.0-hpc/run.sh"<<EOF
#!/bin/bash
yum install -y boost-iostreams.aarch64
yum install -y boost-serialization.aarch64
yum install -y sqlite-devel.aarch64
yum install -y suitesparse-devel
yum install -y mysql5 mysql5-devel
yum install -y gsl-devel.aarch64
yum install -y m4 environment-modules systemd-devel time automake wget tar gcc-c++ git
 
pwd_path=\$('pwd')
source /etc/profile &> /dev/null 
current_dir=\$(cd "\$(dirname "\${BASH_SOURCE[0]}")" &> /dev/null && pwd)
for d in \$(ls \$current_dir)
do
    [ ! -f "\$current_dir/\$d" ] && cd \$current_dir/\$d && module use \$(pwd) && module load \$(pwd)/*modulefiles
done

cd \$pwd_path

EOF

cd pkg/ && tar zcvf Augustus-3.4.0-hpc.tar.gz Augustus-3.4.0-hpc

rm -rf $buildpath $installpath
